<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="js/config.js"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
</head>


<body>
<div class="x-body">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">活动主题</label>
            <div class="layui-input-inline">
                <input type="text" name="theme" required lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">起止时间</label>
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" name="begin_time" required lay-verify="required" class="layui-input" id="begin_time">
            </div>

            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" name="end_time" required lay-verify="required" class="layui-input" id="end_time">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">分享链接</label>
            <div class="layui-input-inline">
                <input type="text" name="share_url" autocomplete="off" required lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>

            <label class="layui-form-label">分享按钮链接</label>
            <div class="layui-input-inline">
                <input type="text" name="sharebtn_url"  required lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">

            <label class="layui-form-label">领取按钮链接</label>
            <div class="layui-input-inline">
                <input type="text" name="receivebtn_url" required lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input">
            </div>

            <label class="layui-form-label">背景链接</label>
            <div class="layui-input-inline">
                <input type="text" name="background_url" required lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input">
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">活动背景颜色</label>
            <div class="layui-input-inline" id="color_picker"></div>
            <input type="text" name="background_color" required lay-verify="required" class="layui-input layui-hide">
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">添加优惠券</label>
            <div class="layui-input-inline" id="add_coupon" autocomplete="off">
                <input type="text" placeholder="券号" name="coupons_key_list" class="layui-input" lay-verify="required">
                <input type="text" placeholder="数量" class="layui-input" lay-verify="required">
                <input type="text" placeholder="金额" class="layui-input" lay-verify="required">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">有效期</label>
            <div class="layui-input-inline">
                <select lay-filter="end_type">
                    <option value="1">固定结束时间</option>
                    <option value="2">固定持续时间</option>
                </select>
            </div>

            <div class="layui-input-inline">
                <div id="end_type_content">
                    <input type="text" name="deadline" class="layui-input" lay-verify="required" id="deadline">
                </div>
            </div>


        </div>

        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="form">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
    <script>
        //时间选择器
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#begin_time' //指定元素
                , type: 'datetime'
                , mini: new Date()
            });
            laydate.render({
                elem: '#end_time' //指定元素
                , type: 'datetime'
            });
            laydate.render({
                elem: '#deadline' //指定元素
                , type: 'datetime'
            });

        });


        //颜色取色器
        layui.use('colorpicker', function () {
            var colorpicker = layui.colorpicker;
            colorpicker.render({
                elem: '#color_picker'
                , predefine: true
                , colors: ['#F00', '#0F0', '#00F', 'rgb(255, 69, 0)', 'rgba(255, 69, 0, 0.5)']
                , done: function (color) {
                    $("input[name = 'background_color']").val(color);
                }
            });
        });


        layui.use('form', function () {
            var form = layui.form;

            //监听下拉列表
            form.on('select(end_type)', function (data) {
                if (data.value == 1) {
                    $('#end_type_content').html('<input type="text" name="deadline" class="layui-input" lay-verify="required" id="deadline">');
                    layui.use('laydate', function () {
                        var laydate = layui.laydate;
                        laydate.render({
                            elem: '#deadline' //指定元素
                            , type: 'datetime'
                        });
                    });
                } else {
                    $('#end_type_content').html('<input type="text" name="duration" class="layui-input" lay-verify="required">');
                }
            });

            //监听提交
            form.on('submit(form)', function (data) {
                var coupons_key_list = [];
                var arr = $('[name="coupons_key_list"]');
                arr.each(function (index, item) {
                    coupons_key_list.push(new coupons($(this).val(), $(this).next().val(), $(this).next().next().val()));
                });
                data.field.coupons_key_list = coupons_key_list;
                $.ajax({
                    url: "http://"+IPADDRESS+":"+PORT+"/activity/putActivity/",
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(data.field),
                    dataType: "json",
                    success: function (data) {
                        if (data.header.res_code == '200002') {
                            layer.alert(data.header.message, {
                                icon: 5,
                                title: "提示"
                            });
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        } else {
                            layer.alert(data.header.message, {
                                icon: 5,
                                title: "提示"
                            });
                        }
                    }
                })
                return false;
            });
        });

    </script>

    <script>
        function coupons(coupons_key, total, amount) {
            this.coupons_key = coupons_key;
            this.total = total;
            this.amount = amount;
        }
    </script>

</div>

</div>
</body>
</html>
